<?php

namespace app\exception;

use think\exception\ErrorException;
use think\exception\Handle;
use think\facade\Lang;
use think\facade\Log;
use think\Response;
use Throwable;

class HandleException extends Handle
{
    public function render($request, Throwable $e): Response
    {
        //数据库
//        dump($e);
//        die;
        return json(['code'=>500,'msg'=>$e->getMessage(),'trace'=>['code'=>$e->getCode(),'file'=>$e->getFile(),'line'=>$e->getLine(),'trace'=>$e->getTrace()]],200);

        if($e instanceof \think\db\exception\PDOException){
            //return json(['code'=>DB_ERROR,'msg'=>Lang::get('DB_ERROR')],500);
        }

        if($e instanceof \think\db\exception\DbException){
            //return json(['code'=>DB_ERROR,'msg'=>Lang::get('DB_ERROR')],500);
        }

        if($e instanceof \think\db\exception\InvalidArgumentException){
            //return json(['code'=>DB_ERROR,'msg'=>Lang::get('DB_ERROR')],500);
        }

        //验证

        if($e instanceof \think\exception\ValidateException){
            //return json(['code'=>PARAM_CHECK_ERROR,'msg'=>$e->getError()],200);
        }

        if($e instanceof \think\exception\InvalidArgumentException){
            //return json(['code'=>PARAM_CHECK_ERROR,'msg'=>Lang::get('PARAM_CHECK_ERROR')],400);
        }

        //系统

        if($e instanceof \think\exception\ClassNotFoundException){
            //return json(['code'=>CLASS_NOT_FOUND,'msg'=>Lang::get('CLASS_NOT_FOUND')],500);
        }

        if($e instanceof \think\exception\RouteNotFoundException){
            //return json(['code'=>ROUTE_NOT_FOUND,'msg'=>Lang::get('ROUTE_NOT_FOUND')],500);
        }

        if($e instanceof \think\exception\HttpException){
            //return json(['code'=>HTTP_ERROR,'msg'=>Lang::get('HTTP_ERROR')],500);
        }

        if($e instanceof \think\exception\ErrorException){
            //return json(['code'=>SYS_ERROR,'msg'=>Lang::get('SYS_ERROR')],500);
        }

        if($e instanceof \ParseError){
            //return json(['code'=>PARSE_ERROR,'msg'=>Lang::get('PARSE_ERROR')],500);
        }

        if($e instanceof \TypeError){
            //return json(['code'=>TYPE_ERROR,'msg'=>Lang::get('TYPE_ERROR')],500);
        }

        if($e instanceof \think\exception\FileException){
            //return json(['code'=>FILE_ERROR,'msg'=>Lang::get('FILE_ERROR')],500);
        }

        if($e instanceof \think\exception\FuncNotFoundException){
            //return json(['code'=>FUNC_NOT_FOUND,'msg'=>Lang::get('FUNC_NOT_FOUND')],500);
        }

        if($e instanceof \think\exception\HttpResponseException){
            //return json(['code'=>HTTP_RESPONSE_ERROR,'msg'=>Lang::get('HTTP_RESPONSE_ERROR')],500);
        }

        //托底

        //return json(['code'=>CM_ERROR,'msg'=>Lang::get('CM_ERROR')],500);

        //return parent::render($request, $e); // TODO: Change the autogenerated stub
    }
}
